﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Linq;
using IssueTracker.Business.Entity;

namespace IssueTracker.Data
{
    public class ProjectCustomTypeDataAccess : DataAccessComponent
    {
        private IssueTrackerDBDataContext dc;

        public ProjectCustomType Create(ProjectCustomType projectCustomType)
        {
            ProjectCustomType temp = base.Clone < ProjectCustomType >(projectCustomType);

            using (dc = new IssueTrackerDBDataContext())
            {
                dc.ObjectTrackingEnabled = true;

                dc.ProjectCustomTypes.InsertOnSubmit(temp);

                try
                {
                    dc.SubmitChanges();

                    projectCustomType = base.Clone<ProjectCustomType>(temp);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return projectCustomType;
        }

        public ProjectCustomType Retrieve(int id)
        {
            using (dc = new IssueTrackerDBDataContext())
            {

                var list = from proj in dc.ProjectCustomTypes
                           where proj.Id == id
                           select proj;

                if (list.Count<ProjectCustomType>() > 0)
                {
                    ProjectCustomType obj = list.First<ProjectCustomType>();
                    return obj;
                }
                else
                    return null;
            }
        }

        public ProjectCustomType Update(ProjectCustomType projectCustomType)
        {
            ProjectCustomType temp = base.Clone<ProjectCustomType>(projectCustomType);

            using (dc = new IssueTrackerDBDataContext())
            {
                dc.ObjectTrackingEnabled = true;
                dc.ProjectCustomTypes.Attach(temp, true);

                try
                {
                    dc.SubmitChanges();

                    projectCustomType = base.Clone<ProjectCustomType>(temp);
                }
                catch (ChangeConflictException ccex)
                {
                    throw new ApplicationException("The project member record that you are trying to update maybe out-dated. Please refresh and try again.", ccex);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return projectCustomType;

        }

        public void Delete(ProjectCustomType projectCustomType)
        {

            ProjectCustomType temp = base.Clone<ProjectCustomType>(projectCustomType);

            using (dc = new IssueTrackerDBDataContext())
            {
                dc.ProjectCustomTypes.Attach(temp);

                dc.ProjectCustomTypes.DeleteOnSubmit(temp);

                try
                {
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        public IEnumerable<ProjectCustomType> List()
        {
            IEnumerable<ProjectCustomType> returnList = null;

            using (dc = new IssueTrackerDBDataContext())
            {

                returnList = dc.ProjectCustomTypes;

                if (returnList != null)
                    return returnList.ToList();
                else
                    return null;
            }
        }

        public IEnumerable<ProjectCustomType> ListByReferenceId(int reference)
        {
            using (dc = new IssueTrackerDBDataContext())
            {
                var list = from proj in dc.ProjectCustomTypes
                           where proj.ProjectId.Equals(reference)
                           select proj;

                return list;
            }
        }

   
    }
}
